package com.itheima.tlias84.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.dto.ReportEmpGenderDTO;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.service.ReportService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    EmpMapper empMapper;

    @Override
    public List<ReportEmpGenderDTO> countEmpByGender() {
        return empMapper.countEmpByGender();
    }

    @Override
    public ResultUtil countEmpByJob() {
        List<Integer> jobs = empMapper.countEmpByJob();
        //统计每种职位有多少员工
        Map<String, List<Integer>> map = jobs.stream().collect(Collectors.groupingBy(t -> {
            return switch (t) {
                case 1 -> "班主任";
                case 2 -> "讲师";
                case 3 -> "学工主管";
                case 4 -> "教研主管";
                case 5 -> "咨询师";
                default -> "其他";
            };
        }));
        Set<String> jobList = map.keySet();
        List<Integer> dataList = map.values().stream().map((t) -> t.size()).toList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jobList",jobList);
        jsonObject.put("dataList",dataList);
        return ResultUtil.success(jsonObject);
    }
}
